package com.ahm.server.controller;


import com.ahm.server.pojo.Admin;
import com.ahm.server.service.IAdminService;
import com.ahm.server.service.IRoleService;
import com.ahm.server.service.impl.AdminServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import sun.misc.JavaAWTAccess;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ahm
 * @since 2021-09-22
 */
@RestController
@RequestMapping("/admin")
@Api(tags = "用户模块")
public class AdminController {

    @Resource
    private IAdminService iAdminService;

    @Resource
    private IRoleService iRoleService;


    @GetMapping("info")
    public Admin getInfo(Authentication authentication){
        Admin admin = (Admin) authentication.getPrincipal();
        if (admin ==null){
            return null;
        }
        String username = admin.getUsername();
        Admin user = iAdminService.getOne(new QueryWrapper<Admin>().eq("username", username));
        user.setPassword(null);
        user.setRoleList(iRoleService.getRoles(admin.getId()));
        return user;

    }


    @GetMapping("/{id}")
    public Admin findAdminById(@PathVariable Integer id){

        return iAdminService.getById(id);
    }

    @GetMapping("/name/{username}")
    public Admin findAdminByUserName(@PathVariable String username){
        return iAdminService.getOne(new QueryWrapper<Admin>().eq("username",username));
    }






}
